package com.dao.impl;

import com.dao.TeacherDao;
import com.entity.Teacher;
import com.entity.TeacherExample;
import com.mapper.TeacherMapper;
import com.page.PageInfo;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository("teacherDao")
public class TeacherDaoImp implements TeacherDao {
    @Resource
    TeacherMapper teacherMapper;

    @Override
    public List<Teacher> getAll() throws Exception {
        return teacherMapper.getAll();
    }

    @Override
    public int addTeacher(Teacher teacher) throws Exception {
        return teacherMapper.insertSelective(teacher);
    }
    //如果在mycat 缓存管理器中 找得到 和cacheid匹配缓存，只查缓存 不执行dao的方法
    @Cacheable(value = "myCache",key = "#cacheId")
    public List<Teacher> getTeachersByPage(PageInfo pageInfo,String cacheId) throws Exception{
        System.out.println("不执行缓存");
        System.out.println("第"+pageInfo.getCurrPage()+"页可能走缓存");
        //将用户选择 当前页码和每页大小传入给map
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) teacherMapper.countByExample(new TeacherExample()));
        //return teacherMapper.getTeachersByPage(map);
        return null;
    }

    //如果在mycat 缓存管理器中 找得到 和cacheid匹配缓存，只查缓存 不执行dao的方法
    public List<Teacher> getTeachersByPage(PageInfo pageInfo) throws Exception{
        System.out.println("第"+pageInfo.getCurrPage()+"页不走缓存");
        //将用户选择 当前页码和每页大小传入给map
        int currPage = pageInfo.getCurrPage();
        int pageSize = pageInfo.getPageSize();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("currPage", currPage);
        map.put("pageSize", pageSize);
        System.out.println(pageInfo.getCurrPage() + " "+ pageInfo.getPageSize());
        //调用示例查询得到当前的总记录数
        pageInfo.setItemCount((int) teacherMapper.countByExample(new TeacherExample()));
        //return teacherMapper.getTeachersByPage(map);
        return null;
    }
    @Cacheable(value = "myCache",key = "#cacheId")
    public int getItemCount(String cacheId){
        return (int)teacherMapper.countByExample(new TeacherExample());
    }


    public List<Teacher> searchTeacherByName(String name,PageInfo pageInfo) throws Exception{
        TeacherExample teacherExample = new TeacherExample();
        int limit = pageInfo.getPageSize();
        int offset = (pageInfo.getCurrPage() - 1) * limit;
        teacherExample.setOffset(offset);//就是原来的firstResult 就是通过（currPage - 1） * pageSize
        teacherExample.setLimit(limit);
        //criteria条件查询接口，添加条件

        //teacherExample.createCriteria().andNameLike(name);
        pageInfo.setItemCount((int)teacherMapper.countByExample(teacherExample));
      return teacherMapper.selectByExample(teacherExample);
    }

    public Teacher selectTeacherWithScsAndCourse(String sno)throws Exception{
        //return teacherMapper.selectTeacherWithScsAndCourse(sno);
        return null;
    }

    public Teacher selectTeacherBySno(String sno){
        return teacherMapper.selectByPrimaryKey(sno);
    }

    public int updateTeacherByCno(Teacher teacher){
        return teacherMapper.updateByPrimaryKeySelective(teacher);
    }

    @Override
    public Teacher loginTeacherByTname(String tname) throws Exception {
        // TODO Auto-generated method stub
        return teacherMapper.loginTeacherByTname(tname);
    }
}
